package com.emergency.db.service.impl.work;

import com.emergency.base.domain.Army;
import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.db.dao.work.ArmyDao;
import com.emergency.db.service.work.ArmyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ArmyServiceImpl implements ArmyService {
    private final static Logger logger = LoggerFactory.getLogger(ArmyServiceImpl.class);

    @Autowired
    private ArmyDao armyDao;

    @Autowired
    private IdWorker idWorker;

    //保存
    public Long save(Army army) {
        if (CommonUtils.isNullOrEmpty(army.getId())) {
            army.setId(idWorker.nextId());
            army.setCreateTime(new Date());
        }
        armyDao.save(army);
        return army.getId();
    }

    //批量保存
    @Override
    public void saveAll(List<Army> armyList) {
        armyList.forEach(army -> {
            if (CommonUtils.isNullOrEmpty(army.getId())) {
                army.setId(idWorker.nextId());
                army.setCreateTime(new Date());
            }
            armyDao.save(army);
        });
    }

    //查询单项
    public Army findById(Long id) {
        Army army = armyDao.findById(id).orElse(null);
        if (CommonUtils.isNullOrEmpty(army)) {
            logger.info("此分队信息为空！");
            return army;
        }
        return army;
    }

    //删除
    public Long delete(Long id) {
        armyDao.deleteById(id);
        return id;
    }

    //查询全部
    @Override
    public List<Army> findAll() {
        return armyDao.findAll();
    }

    //分页查询
    @Override
    public Page<Army> findList(Map searchMap, int page, int size) {
        Specification<Army> specification = createSpecification(searchMap);
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        return armyDao.findAll(specification, pageRequest);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Army> createSpecification(Map searchMap) {
        return new Specification<Army>() {

            @Override
            public Predicate toPredicate(Root<Army> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<Predicate>();

                // 姓名
                if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                    predicateList.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + (String) searchMap.get("name") + "%"));
                }
                //分队
                if (searchMap.get("army") != null && !"".equals(searchMap.get("army"))) {
                    predicateList.add(criteriaBuilder.like(root.get("army").as(String.class), "%" + (String) searchMap.get("army") + "%"));
                }
                //分队名下小组
                if (searchMap.get("teamGroup") != null && !"".equals(searchMap.get("teamGroup"))) {
                    predicateList.add(criteriaBuilder.like(root.get("teamGroup").as(String.class), "%" + (String) searchMap.get("teamGroup") + "%"));
                }
                //分组名下小分队
                if (searchMap.get("team") != null && !"".equals(searchMap.get("team"))) {
                    predicateList.add(criteriaBuilder.like(root.get("team").as(String.class), "%" + (String) searchMap.get("team") + "%"));
                }
                //小分队名下集合
                if (searchMap.get("teamSet") != null && !"".equals(searchMap.get("teamSet"))) {
                    predicateList.add(criteriaBuilder.like(root.get("teamSet").as(String.class), "%" + (String) searchMap.get("teamSet") + "%"));
                }
                //隶属
                if (searchMap.get("func") != null && !"".equals(searchMap.get("func"))) {
                    predicateList.add(criteriaBuilder.like(root.get("func").as(String.class), "%" + (String) searchMap.get("func") + "%"));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

}
